<?php
namespace app\admin\controller;
use think\Db;
use lib\Curl;
class System extends Base{
    //广告图
    public function system_set(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('system_set')->where('id',1)->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['site_title']=$param['site_title'];
            $data['seo_keywords']=$param['seo_keywords'];
            $data['seo_description']=$param['seo_description'];
            $data['tel']=$param['tel'];
            $data['copyright']=$param['copyright'];
            $data['icp']=$param['icp'];
            $data['fax']=$param['fax'];
            $data['qrcode_wechat']=$param['qrcode_wechat'];
            $data['logo']=$param['logo'];
            $data['longitude_latitude']=$param['longitude_latitude'];
            $data['address']=$param['address'];
            $data['email']=$param['email'];
            $data['qq']=$param['qq'];
            $data['wechat']=$param['wechat'];
            $data['contact_phone']=$param['contact_phone'];
            $res= Db::name('system_set')->where(array('id'=>1))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }

    public function headquarters_branches(){
        $list=Db::name('headquarters_branches')->order('id desc')->paginate(10);
        $counts=Db::name('headquarters_branches')->count();
        $data=$list->all();
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('counts',$counts);
        return $this->fetch();
    }
    public function headquarters_branches_add(){
        if(request()->isGet()){
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['company_name']=$param['company_name'];
            $data['company_address']=$param['company_address'];
            $data['site_url']=$param['site_url'];
            $data['tel']=$param['tel'];
            $data['email']=$param['email'];
            $data['phone']=$param['phone'];
            $data['sort']=$param['sort'];
            $res= Db::name('headquarters_branches')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function headquarters_branches_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('headquarters_branches')->where('id',$param['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['company_name']=$param['company_name'];
            $data['company_address']=$param['company_address'];
            $data['site_url']=$param['site_url'];
            $data['tel']=$param['tel'];
            $data['email']=$param['email'];
            $data['phone']=$param['phone'];
            $data['sort']=$param['sort'];
            $res=Db::name('headquarters_branches')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function headquarters_branches_del(){
        $param = input('post.');
        $res= Db::name('headquarters_branches')->where(array('id'=>$param['id']))->delete();
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function others(){
        $keyword=input('get.keyword');
        $state=input('get.state');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $map = [
            ['time', '>', $fromtime],
            ['time', '<', $totime]
        ];
        if($state==0){
            array_push($map,['state','<',3]);
        }else{
            array_push($map,['state','=',$state]);
        }
        $list=Db::name('others')->where($map)->where('others','like',"%{$keyword}%")->order('id desc')->paginate(10);
        $counts=Db::name('others')->where($map)->where('others','like',"%{$keyword}%")->count();
        $data=$list->all();
        $this->assign('keyword',input('get.keyword'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('state',$state);
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('counts',$counts);
        return $this->fetch();
    }
    public function others_add(){
        if(request()->isGet()){
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['others']=$param['others'];
            $data['link']=$param['link'];
            $data['state']=$param['state'];
            $data['sort']=$param['sort'];
            $data['time']=time();
            $res= Db::name('others')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function others_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('others')->where('id',$param['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['others']=$param['others'];
            $data['link']=$param['link'];
            $data['state']=$param['state'];
            $data['sort']=$param['sort'];
            $res=Db::name('others')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function others_del(){
        $param = input('post.');
        $res= Db::name('others')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('others')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('others')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    
}
